package cn.com.bluemoon.daps.model.service.impl;

import cn.com.bluemoon.daps.common.enums.BmStatus;
import cn.com.bluemoon.daps.model.dto.DapModelHistoryDto;
import cn.com.bluemoon.daps.model.entity.DapDataModel;
import cn.com.bluemoon.daps.model.entity.DapDataModelOperateHistory;
import cn.com.bluemoon.daps.model.entity.DapDataModelTableScoreRule;
import cn.com.bluemoon.daps.model.mapper.DapDataModelOperateHistoryMapper;
import cn.com.bluemoon.daps.model.service.DapDataModelOperateHistoryService;
import cn.com.bluemoon.daps.model.vo.SearchModelHistoryVo;
import cn.com.bluemoon.daps.model.vo.SearchRecordTableVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 申请发布版本记录表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-08-03
 */
@Slf4j
@Service
public class DapDataModelOperateHistoryServiceImpl extends ServiceImpl<DapDataModelOperateHistoryMapper, DapDataModelOperateHistory> implements DapDataModelOperateHistoryService {

    @Resource
    DapDataModelOperateHistoryMapper dapDataModelOperateHistoryMapper;

    /**
     * 表修改记录
     * @param vo
     * @return
     */
    @Override
    public IPage<DapDataModelOperateHistory> getTableRecordList(SearchRecordTableVo vo) {

        // 分页条件
        Page<DapDataModelOperateHistory> page = new Page<>(vo.getPageNum(), vo.getPageSize());
        Page<DapDataModelOperateHistory> pages = dapDataModelOperateHistoryMapper.selectTableHistory(page, vo);
        return pages;
    }

    /**
     * 查看表的详细修改信息
     * @return
     */
    @Override
    public List<DapDataModelOperateHistory> getTableModifyList(String modelTableId, String applicationTime) {

        List<DapDataModelOperateHistory> dapDataModelOperateHistories = dapDataModelOperateHistoryMapper.selectList(new QueryWrapper<DapDataModelOperateHistory>()
                .eq("model_table_id", modelTableId).eq("bm_status", BmStatus.ON.getCode())
                .apply(StringUtils.isNotBlank(applicationTime), "public_time <= date_format('" + applicationTime + "','%Y-%m-%d-%T')"));
        return dapDataModelOperateHistories;
    }

    @Override
    public void updateRecordId(String recordId, String modelId) {

        dapDataModelOperateHistoryMapper.updateRecordId(recordId,modelId);
    }

    @Override
    public IPage<DapModelHistoryDto> getModelHistory(SearchModelHistoryVo vo) {

        Page<DapDataModel> page = new Page<>(vo.getPageNum(), vo.getPageSize());
        IPage<DapModelHistoryDto> dapModelHistoryDtoIPage = dapDataModelOperateHistoryMapper.selectModelHistoryList(page, vo);
        return dapModelHistoryDtoIPage;
    }
}
